package pl.polidea.test;

import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;

import pl.polidea.test.ZoomView.ZoomViewListener;
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.FrameLayout;
import android.widget.FrameLayout.LayoutParams;

public class ImageListActivity extends Activity {
    private ArrayList<File> listFiles(ArrayList<File> files) {
        ArrayList<File> result = new ArrayList<File>();

        for (int i = 0; i < files.size(); i++) {
            if (files.get(i).isDirectory()) {
                File file = files.get(i);
                files.remove(i);
                File[] files2 = file.listFiles(filter);
                if (files2 == null)
                    continue;
                ArrayList<File> f = new ArrayList<File>();
                for (int j = 0; j < files2.length; j++) {
                    f.add(files2[j]);
                }
                result.addAll(listFiles(f));
            } else {
                result.add(files.get(i));
            }
        }

        return result;
    }

    FileFilter filter = new FileFilter() {

        @Override
        public boolean accept(File arg0) {
            return arg0.getName().endsWith(".jpg") || arg0.getName().endsWith(".bmp")
                    || arg0.getName().endsWith(".png") || arg0.isDirectory();
        }

    };

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final ZoomView zv = new ZoomView(this);
        setContentView(zv, new FrameLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT,
                FrameLayout.LayoutParams.FILL_PARENT));

        ArrayList<File> files = new ArrayList<File>();
        files.add(new File("/sdcard/"));
        files = listFiles(files);

        zv.setAnimationCacheEnabled(false);
        zv.setMaxZoom(20);
        FrameLayout fl = new FrameLayout(this);
        final int photos = 200;
        final NativeImageView[] images = new NativeImageView[Math.min(files.size(), photos)];
        zv.setListner(new ZoomViewListener() {

            @Override
            public void onZooming(float zoom, float zoomx, float zoomy) {
                for (int i = 0; i < images.length; i++) {
                    images[i].setMipLevel((int) (Math.log(20 - zoom) / Math.log(2)) - 1);
                }
            }

            @Override
            public void onZoomStarted(float zoom, float zoomx, float zoomy) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onZoomEnded(float zoom, float zoomx, float zoomy) {
                // TODO Auto-generated method stub

            }
        });
        for (int i = 0; i < images.length; i++) {
            NativeImageView img = new NativeImageView(this, null);
            LayoutParams layoutParams = new FrameLayout.LayoutParams(80, 60, Gravity.LEFT);
            layoutParams.leftMargin = (int) (80 * (i / Math.round(Math.sqrt(Math.min(files.size(), photos)))));
            layoutParams.topMargin = (int) (60 * (i % Math.round(Math.sqrt(Math.min(files.size(), photos)))));
            fl.addView(img, layoutParams);
            images[i] = img;
        }
        final ArrayList<File> files2 = files;
        Thread thread = new Thread() {
            public void run() {
                for (int i = 0; i < images.length; i++) {
                    NativeImageView img = images[i];
                    img.setImageBitmap(files2.get(i).getPath());
                    zv.postInvalidate();
                }
            }
        };
        thread.setPriority(Thread.MIN_PRIORITY);
        thread.setDaemon(true);
        thread.start();
        zv.addView(fl, new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT,
                FrameLayout.LayoutParams.WRAP_CONTENT));
    }

    @Override
    protected void onResume() {
        // baseAdapterExtension.allocateAll();
        super.onResume();
    }

    @Override
    protected void onPause() {
        // baseAdapterExtension.deallocateAll();
        super.onPause();
    }
}